<?php
session_start();
include_once 'inc/classes.inc.php';
if(empty($_SESSION)){
    header('location: index.php');
}
else{
    if(!isset($_SESSION['user']))
        header('location: index.php');
    else{
        $avisDAO = new AvisDAO();
        $userDAO = new UserDAO();
        $produitDAO = new ProduitDAO();
        $lesAvis = array();
        $user = unserialize($_SESSION['user']);
        
        if(isset($_POST['subChangeCoord'])){
             $msgChangeCoord = FormGeneral::changerCoordUser($user, $_POST);
             if($msgChangeCoord->getStatut() == 1){
                 $user = $userDAO->getUserById($user->getId());
                 $_SESSION['user'] = serialize($user);
             }
        }
        if(isset($_POST['subChangeMdp'])){
            $msgChange = FormGeneral::changerMdp($user, $_POST);
            if($msgChange->getStatut() == 1){
                 $user = $userDAO->getUserById($user->getId());
                 $_SESSION['user'] = serialize($user);
            }
        }
         if(isset($_POST['subFormChangeAvis'])){
             $idavis = $_POST['idavis'];
             $comment = $_POST['newCommentaireAvis'];
             $note = $_POST['newNoteAvis'];
             $avisDAO = new AvisDAO();
             $oldAvis = $avisDAO->getAvisById($idavis);
             if(is_numeric($note) && $comment != ""){
                 $oldAvis->setNote($note);
                 $oldAvis->setCommentaire($comment);
                 if($avisDAO->modifierAvis($oldAvis)){
                    $msgChangeAvis = "avis modifié";
                 }
                 
             }
             else{
                 $msgChangeAvis = "les champs sont vides";
             }
        }
        $lesAvis = $avisDAO->getLesAvisByIdAuteur($user->getId());
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <?php
            include_once 'inc/entete.php';
        ?>
    </head>
    <body>
        <?php
            include_once 'inc/modal.php';
            include_once 'inc/header.php';
        ?>
        <div id="conteneurProfil">
            <div class="span3 bs-docs-sidebar">
                <ul class="nav nav-list bs-docs-sidenav affix">
                    <li><a href="#">Profil de <strong><?php echo $user->getPrenom()." ".$user->getNom() ?></strong></a></li>
                    <li><a href="#voirCoord"><i class="icon-chevron-right"></i>Voir mes coordonnées</a></li>
                    <li><a href="#modalChangeCoord" data-toggle="modal"><i class="icon-chevron-right"></i>Modifier coordonnées</a></li>
                    <li><a href="#modalChangeMdp" data-toggle="modal"><i class="icon-chevron-right"></i>Modifier mon mot de passe</a></li>
                    <li><a href="#voirAvisEnAttente"><i class="icon-chevron-right"></i>Voir mes avis en attente de validation</a></li>
                    <li><a href="#voirAvisValide"><i class="icon-chevron-right"></i>Voir mes avis validés</a></li>
                    <li><a href="#modAvis"><i class="icon-chevron-right"></i>Modifier mes Avis</a></li>
                </ul>
            </div>
            <div id="lesCategprofil" class="span9">
                <div id="voirCoord">
                        <table class="table table-striped table-hover table-bordered">
                            <tbody>
                                <?php
                                    echo "<tr><td>Prénom</td><td>".$user->getPrenom()."</td></tr>";
                                    echo "<tr><td>Nom </td><td>".$user->getNom()."</td></tr>";
                                    echo "<tr><td>Login </td><td>".$user->getLogin()."</td></tr>";
                                    echo "<tr><td>Adresse Email</td><td>".$user->getEmail()."</td></tr>";
                                    echo "<tr><td>Date de naissance</td><td>".$user->getDateNaissance()."</td></tr>";
                                    echo "<tr><td>Type d'utilisateur</td><td>".$type = $user->estAdmin() == 1 ? 'admin' : 'user'."</td></tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                <div id="voirAvisEnAttente">
                    <h3>Les avis en attente</h3>
                    <?php
                        if(isset($lesAvis)){
                            $isEmpty = true; 
                            echo "<ul class='imgplusavis'>";
                            foreach ($lesAvis as $avis){
                                
                                if($avis->estValide() == 0){
                                    $isEmpty = false;
                                    $produit = $produitDAO->getProduitByIdAvis($avis->getId());
                                   
                                    echo "<div class='avis well'><a href='visuproduit.php?idp=".$produit->getReference()."'><img src='../".$produit->getCheminImage()."' width='125'></a>";
                                        echo "<p class='header'><span class='label label-info'>".$avis->getNote()."/5</span> le ".$avis->getDatePublication()."
                                                <a class='btn btn-danger pull-right' href='sup.php?ida=".$avis->getId()."' onclick='return confirm(\"Confirmez la suppression de cet avis ?\");'>
                                                    <i class='icon-trash icon-white'></i>&nbsp;
                                                </a>&nbsp;
                                                 <a class='btn btn-warning pull-right showModalChangeAvis' data-ida='".$avis->getId()."' data-note ='".$avis->getNote()."' data-comment='".$avis->getCommentaire()."' onclick='return false;'>
                                                    <i class='icon-edit icon-white'></i>&nbsp;
                                                </a>
                                            </p>";
                                        echo "<p>".$avis->getCommentaire()."</p>";
                                    echo "</div>";
                                }
                            }
                            if($isEmpty){
                                echo "<p class='alert'>aucun avis en attente de validation</p>";
                            }
                            
                        }
                        
                    ?>
                </div>
                <div id="voirAvisValide">
                    <h3>Les avis validés</h3>
                     <?php
                        if(isset($lesAvis)){
                            $isEmpty = true;
                            foreach ($lesAvis as $avis){
                                if($avis->estValide() == 1){
                                    $isEmpty = false;
                                    $produit = $produitDAO->getProduitByIdAvis($avis->getId());
                                    echo "<div class='avis well'><a href='visuproduit.php?idp=".$produit->getReference()."'><img src='../".$produit->getCheminImage()."' width='125'></a>";
                                        echo "<p class='header'><span class='label label-info'>".$avis->getNote()."/5</span>&nbsp; le ".$avis->getDatePublication()."
                                                <a class='btn btn-danger pull-right' href='sup.php?ida=".$avis->getId()."' onclick='return confirm(\"Confirmez la suppression de cet avis ?\");'>
                                                    <i class='icon-trash icon-white'></i>
                                                </a>
                                            </p>";
                                        echo "<p>".$avis->getCommentaire()."</p>"; 
                                    echo "</div>";
                                }
                            }
                            if($isEmpty){
                                echo "<p class='alert'>aucun avis en attente de validation</p>";
                            }
                        }   
                    ?>
                </div>
                <div id="modAvis">
                   
                </div>
            </div>
        </div>
        
        <!-- LES MODALS -->
        <div class="modal hide" id="modalChangeMdp">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>Changement de mot de passe</h3>
            </div>
            <div class="modal-body">
                <form id="formChangeMdp" action="" method="POST">
                    <p><input type="password" name="oldmdp" placeholder="votre ancien mot de passe"></p>
                    <p><input type="password" name="newMdp" placeholder="nouveau mot de passe"></p>
                    <p><input type="password" name="newMdp2" placeholder="retapez votre mot de passe"></p>
                    <p><input type="submit" name="subChangeMdp" class="submitCool btn btn-primary" value="Changer"></p>
                <?php 
                    if(isset($msgChange)){
                        $classeCss = $msgChange->getStatut() == 0 ? "alert alert-error" : "alert alert-success";
                            echo "<p class='" . $classeCss . " submitCool '>" . $msgChange->getMessage() . "</p> ";
                            if($msgChange->getStatut() == 1){
                                echo "<script>enleverModal('modalChangeMdp', 1000);</script>";
                            }
                    }
                ?>
                </form>
            </div>
        </div>
        <?php
            if (isset($msgChange)) {
                echo "<script> $('#modalChangeMdp').modal();  </script>";
            }
        ?>
        
        </div>
        <div class="modal hide" id="modalChangeCoord">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>modifier ses informations</h3>
            </div>
            <div class="modal-body">
                <form id="formChangeCoord" action="" method="POST" class="formClassy">
                    <ul>
                        <li><input type="text" name="newPrenom" placeholder="prenom" value="<?php echo $user->getPrenom(); ?>"></li>
                        <li><input type="text" name="newNom" placeholder="nom" value="<?php echo $user->getNom(); ?>"></li>
                        <li><input type="text" name="newLogin" placeholder="login" value="<?php echo $user->getLogin(); ?>"></li>
                        <li><input type="text" name="newEmail" placeholder="email" value="<?php echo $user->getEmail(); ?>"></li>
                        <li><input type="text" name="newDateNaissance" placeholder="date de naissance" value="<?php echo $user->getDateNaissance(); ?>"></li>
                        <li><input type="submit" name="subChangeCoord" value="Changer" class="btn btn-primary submitCool"></li>
                    </ul>
                <?php 
                    if(isset($msgChangeCoord)){
                        $classeCss = $msgChangeCoord->getStatut() == 0 ? "alert alert-error" : "alert alert-success";
                            echo "<p class='" . $classeCss . " submitCool '>" . $msgChangeCoord->getMessage() . "</p> ";
                            
                            if($msgChangeCoord->getStatut() == 1){
                                echo "<script>enleverModal('modalChangeCoord', 1000);</script>";
                            }
                    }
                ?>
                </form>
            </div>
        </div>
        <?php
            if (isset($msgChangeCoord)) {
                echo "<script> $('#modalChangeCoord').modal();  </script>";
            }
        ?>
        
        <div class="modal hide" id="modalChangeAvis">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>modifier un avis</h3>
            </div>
            <div class="modal-body">
                <form id="formChangeAvis" action="" method="POST" class="formClassy">
              <ul>
                <li>
                    <select name="newNoteAvis" id="newNoteAvis">
                        <option value="dis" disabled selected="selected">Donnez une note</option>
                        <option value="0">0</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                    </select>
                </li>
                <li>
                    <input type="hidden" id="idavis" name="idavis" value="<?php if(isset($_POST['idavis'])) echo $_POST['idavis']; ?> ">
                    <textarea name="newCommentaireAvis" id="newCommentaireAvis" placeholder="Votre commentaire"></textarea>
                </li>
                <li><input type="submit" name="subFormChangeAvis" class="btn btn-primary" value="Changer"></li>
            </ul>
                <?php 
                    if(isset($msgChangeAvis)){
                        $classeCss = $msgChangeAvis != "avis modifié" ? "alert alert-error" : "alert alert-success";
                            echo "<p class='" . $classeCss . " submitCool '>" . $msgChangeAvis . "</p> ";
                            
                            if($msgChangeAvis == "avis modifié"){
                                echo "<script>enleverModal('modalChangeAvis', 1000);</script>";
                            }
                    }
                ?>
                </form>
            </div>
        </div>
        <?php
        if (isset($msgChangeAvis)) {
                echo "<script> $('#modalChangeAvis').modal();  </script>";
            }
        ?>    
        <script>
            $('.showModalChangeAvis').click(function(){
                var note = $(this).attr('data-note');
                var com = $(this).attr('data-comment');
                var idavis = $(this).attr('data-ida');
                
                $('#newCommentaireAvis').val(com);
                $('#newNoteAvis').val(note);
                $('#idavis').val(idavis);
                
                $('#modalChangeAvis').modal();
                
            });
    
        </script>
    </body>
</html>